package cn.buk.tms.eotms.dto;

import cn.buk.tms.eotms.entity.Customer;
import cn.buk.tms.eotms.entity.TrainRefundOrder;
import cn.buk.tms.eotms.entity.TrainRefundOrderPassenger;
import org.springframework.beans.BeanUtils;

import java.util.ArrayList;

/**
 *
 * @author yfdai
 * @date 2019-10-13
 */
public class TrainRefundOrderDto extends TrainOrderDto {

    public static TrainRefundOrderDto createByOrder(final TrainRefundOrder o) {
        TrainRefundOrderDto dto = new TrainRefundOrderDto();

        BeanUtils.copyProperties(o, dto);

        dto.setPassengers(new ArrayList<>());
        String psgNames = null;
        for(TrainRefundOrderPassenger psg: o.getPassengers()) {

            TrainOrderPassengerDto psgDto = new TrainOrderPassengerDto();

            BeanUtils.copyProperties(psg, psgDto);

            dto.getPassengers().add(psgDto);

            if (psgNames == null) {
                psgNames = psg.getName();
            } else {
                psgNames += "," + psg.getName();
            }
        }

        if (psgNames != null) {
            dto.setPsgCount(dto.getPassengers().size());
            dto.setPassengerName(psgNames);
        }


        if (o.getCustomer() == null) {
            dto.setCustomerId(0);
            dto.setCustomerName("散客");
        } else {
            dto.setCustomerId(o.getCustomerId());
            dto.setCustomerName(o.getCustomer().getVipName());
            dto.setCustomerCode(o.getCustomer().getCustomerCode());
        }


        return dto;
    }

    public static TrainRefundOrder convertToOrder(TrainRefundOrderDto dto) {
        TrainRefundOrder o = new TrainRefundOrder();
        BeanUtils.copyProperties(dto, o);

        o.setPassengers(new ArrayList<>());
        String psgNames = null;
        for(TrainOrderPassengerDto psgDto: dto.getPassengers()) {

            TrainRefundOrderPassenger psg = new TrainRefundOrderPassenger();
            psg.setOrderInfo(o);
            psg.setEnterpriseId(o.getEnterpriseId());

            BeanUtils.copyProperties(psgDto, psg);



            o.getPassengers().add(psg);

            if (psgNames == null) {
                psgNames = psgDto.getName();
            } else {
                psgNames += "," + psgDto.getName();
            }
        }

        o.setPsgCount(o.getPassengers().size());
        o.setPassengerName(psgNames);


        o.setCustomer(null);
        if (dto.getCustomerId() > 0) {
            Customer c = new Customer();
            c.setId(dto.getCustomerId());

            o.setCustomer(c);
            //企业客户
            o.setCustomerType(1);
        } else {
            // 散客
            o.setCustomerType(0);
        }


        return o;
    }

    /**
     * 原火车票订单号
     */
    private String trainOrderNo;

    public String getTrainOrderNo() {
        return trainOrderNo;
    }

    public void setTrainOrderNo(String trainOrderNo) {
        this.trainOrderNo = trainOrderNo;
    }
}
